<template>
	<view>
		<u-cell-group v-if="merchantList.length>0">
			<u-cell :title="item.merchantId" :value="item.merchantShortName" :arrow="false" v-for="item in merchantList"
				:key="item.id" @click="select(item)"></u-cell>
		</u-cell-group>
		<view class="tips" v-else @click="navigateTo">
			您还没绑定收款商户,<text style="color:#007aff;margin: 0 5rpx;">去绑定</text>收款商户
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				from: '',
				merchantList: [],
				status: 'loadmore',
				loadText: {
					loadmore: '轻轻上拉加载',
					loading: '稍等一会,正在努力为您加载...',
					nomore: '没有更多了'
				},
				page: {
					pageNum: 1,
					type: 1,
				},
			};
		},
		onLoad(option) {
			this.from = option.from;
			this.merchantListLoad();
			console.log(this.merchantList)
		},
		onPullDownRefresh() {
			this.merchantList = [];
			this.page.pageNum = 1;
			this.status = 'loadmore';
			this.merchantListLoad();
		},
		methods: {
			merchantListLoad() {
				this.$api('merchant.findMerchantListByUserId', this.page).then(res => {
					if (res.data != null) {
						let _this = this;
						if (res.data.list.length == 0) {
							uni.showModal({
								title: "温馨提示",
								content: "您还没绑定收款商户，请先绑定收款商户,否则无法支付",
								cancelText: '绑定',
								cancelColor: "#007aff",
								confirmText: "取消",
								confirmColor: "#000000",
								success: (res) => {
									if (res.confirm) {
										uni.navigateBack();
									} else {
										let userInfo = _this.$store.getters.GET_VALUE("GET_USER_INFO");
										if (!userInfo.username && !userInfo.mobile) {
											uni.navigateTo({
												url: '/pages/complete-user-info/complete-user-info?type=1'
											})
										} else {
											uni.navigateTo({
												url: '/pages-qualification/add/settlement-type'
											})
										}
									}
								}
							})
						} else {
							uni.stopPullDownRefresh();
							this.merchantList.push(...res.data.list)
							this.page.pageNum = res.data.pageNum;
							if (this.page.pageNum >= res.data.pages) {
								this.status = 'nomore';
								return;
							}
						}
					}
				})
			},
			select(data) {
				if (this.from == 'site') {
					uni.setStorageSync('merchant', data);
					uni.navigateBack()
				}
			},
			navigateTo() {
				let userInfo = this.$store.getters.GET_VALUE("GET_USER_INFO");
				if (!userInfo.username && !userInfo.mobile) {
					uni.navigateTo({
						url: '/pages/complete-user-info/complete-user-info?type=1'
					})
				} else {
					uni.navigateTo({
						url: '/pages-qualification/add/settlement-type'
					})
				}
			}
		},
		// 触底执行
		onReachBottom() {
			console.log("-------------下拉加载--------------")
			if (this.status == 'nomore') {
				return;
			}
			this.status = 'loading';
			// 使用延迟实现加载动画效果
			setTimeout(() => {
				this.page.pageNum = ++this.page.pageNum;
				this.merchantListLoad();
				this.status = 'loadmore';
			}, 2000)
		}
	}
</script>

<style lang="scss">
	.tips {
		display: flex;
		justify-content: center;
		margin-top: 500rpx;
		font-size: 36rpx;
		color: rgba(0, 0, 0, 0.55);
	}
</style>